package com.xjtu.conan.controller;


import com.xjtu.conan.entity.ArticleInfo;
import com.xjtu.conan.entity.NewsInfo;
import com.xjtu.conan.entity.UserInfo;
import com.xjtu.conan.response.AjaxResponse;
import com.xjtu.conan.service.ArticleInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * <p>
 * 话题表 前端控制器
 * </p>
 *
 * @author 一只黑猩猩
 * @since 2021-04-19
 */
@CrossOrigin
@RestController
@RequestMapping("/conan/article-info")
@Api(value = "话题管理模块", description = "这是话题信息CRUD的模块")
public class ArticleInfoController {
    @Autowired
    private ArticleInfoService articleInfoService;

    @ApiOperation(value = "获取全部话题", notes = "获取所有话题的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = false, defaultValue = "1", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页数量", required = false, defaultValue = "10", paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/getAllArticles", method = RequestMethod.GET)
    public AjaxResponse getAllArticles(@RequestParam(value = "current") Integer current,
                                       @RequestParam(value = "size") Integer size) {

        return AjaxResponse.success(articleInfoService.getAllArticleInfo(current, size));
    }

    @ApiOperation(value = "添加话题", notes = "添加一个话题")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "aName", value = "话题标题", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "aContent", value = "话题内容", required = true, paramType = "form", dataType = "String"),
    })
    @RequestMapping(value = "addNews", method = RequestMethod.POST)
    public AjaxResponse addNews(@RequestParam(value = "aName") String aName,
                                @RequestParam(value = "aContent") String aContent,
                                HttpServletRequest request) throws IOException {
        UserInfo loginUser = (UserInfo) request.getSession().getAttribute("loginUser");
        ArticleInfo articleInfo = new ArticleInfo()
                .setArticleName(aName)
                .setArticleContent(aContent)
                .setCreateUser(1384057153556135937L)
//                .setCreateUser(loginUser.getUid())
                .setUpdateUser(1384057153556135937L);
//                .setUpdateUser(loginUser.getUid());
        return AjaxResponse.success("新增话题ID->" + articleInfoService.addArticleInfo(articleInfo));
    }

    @ApiOperation(value = "更新话题", notes = "更新一个话题")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "aName", value = "话题标题", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "aContent", value = "话题内容", required = true, paramType = "form", dataType = "String"),
    })
    @RequestMapping(value = "updateArticle", method = RequestMethod.POST)
    public AjaxResponse updateArticle(@RequestParam(value = "aName") String aName,
                                @RequestParam(value = "aContent") String aContent,
                                HttpServletRequest request) throws IOException {
        UserInfo loginUser = (UserInfo) request.getSession().getAttribute("loginUser");
        ArticleInfo articleInfo = new ArticleInfo()
                .setArticleName(aName)
                .setArticleContent(aContent)
                .setUpdateUser(loginUser.getUid());
        return AjaxResponse.success("新增话题ID->" + articleInfoService.updateArticleInfo(articleInfo));
    }
    @ApiOperation(value = "删除话题信息", notes = "从数据库中删除某个话题")
    @ApiImplicitParam(name = "aid", value = "删除话题id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/deleteArticles", method = RequestMethod.DELETE)
    public AjaxResponse deleteArticles(@RequestParam(value = "aid") Long aid) {
        return AjaxResponse.success("数据库受影响行数->" + articleInfoService.deleteArticleInfo(aid));
    }

    @ApiOperation(value = "话题总数量", notes = "获取数据库中话题总数量")
    @RequestMapping(value = "/getTotal", method = RequestMethod.GET)
    public AjaxResponse getTotal() {
        return AjaxResponse.success(articleInfoService.getTotal());
    }
}
